<?php 
require_once 'common/common.inc.php';
require_once 'common/mail.inc.php';

$service_code = "CAR_WASH";
    
$auto_wassen_datum = isset($_REQUEST["auto_wassen_datum"]) ? $_REQUEST["auto_wassen_datum"] : null;
$auto_wassen_pakket = isset($_REQUEST["auto_wassen_pakket"]) ? $_REQUEST["auto_wassen_pakket"] : null;
$auto_wassen_name1 = isset($_REQUEST["auto_wassen_name1"]) ? $_REQUEST["auto_wassen_name1"] : $name1;
$auto_wassen_tel_number = isset($_REQUEST["auto_wassen_tel_number"]) ? $_REQUEST["auto_wassen_tel_number"] : null;
$auto_wassen_kenteken_auto = isset($_REQUEST["auto_wassen_kenteken_auto"]) ? $_REQUEST["auto_wassen_kenteken_auto"] : null;
$location_name = isset($_REQUEST["location_name"]) ? $_REQUEST["location_name"] : "";;
$error = isset($_REQUEST["error"]) ? $_REQUEST["error"] : "";

$tmp = preg_split("/-/",$auto_wassen_datum);
$auto_wassen_datum_iso = $tmp['2']."-".$tmp['1']."-".$tmp['0'];

$start_date = new DateTimeDutch("@".strtotime($auto_wassen_datum_iso), $timezone);
$start_date->setTimezone($timezone); 

$auto_wassen_pakket_nr;
$auto_wassen_pakket_desc;
$auto_wassen_pakket_price;
autoWassenPakketSwitch();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

  conn();

  $result = mysql_query("SELECT NAME FROM LOCATIONS  WHERE CODE = '$location_code'");
  if (!$result) {die('Could not exec query: ' . mysql_error());}
  $row = mysql_fetch_array($result, MYSQL_ASSOC);
  $location_name = $row["NAME"];
  
  // execute reservation
  if (empty($error)) 
  {

    $sql = "";
    $sql .= "SELECT s.ID, s.NAME, s.START, s.END, DATE_FORMAT(s.START,'%Y-%m-%d') START_SHORT_ISO, COUNT(r.ID) RESERVATION_COUNT ";
    $sql .= "FROM SCHEDULES s ";
    $sql .= "  LEFT OUTER JOIN RESERVATIONS r ON s.ID = r.SCHEDULE_ID ";
    $sql .= "WHERE s.LOCATION_CODE = '$location_code' ";
    $sql .= "AND s.SERVICE_CODE = '$service_code' ";
    $sql .= "AND DATE_FORMAT(s.START,'%Y-%m-%d') = '$auto_wassen_datum_iso' ";
    $sql .= "GROUP BY s.ID, s.NAME, s.START, s.END ";
    
    $result = mysql_query($sql);
    if (!$result) {die('Could not exec query: ' . mysql_error());}
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $schedule_id = $row["ID"];
  
    $guid = uniqid("", true);
    $sql = "INSERT INTO RESERVATIONS (SCHEDULE_ID, SIGN_IN_EMAIL, GUID, START, END, CREATE_DATE) ";
    $sql .= "VALUE ";
    $sql .= "('$schedule_id','$email','$guid','$auto_wassen_datum_iso','$auto_wassen_datum_iso', NOW()) ";
    //echo $sql;

    $result = mysql_query($sql);
    if (!$result) {die('Could not exec query: ' . mysql_error());}
    $reservation_id = mysql_insert_id();
    
    if (empty($reservation_id)) {
      $error = "De reservering kon niet gemaakt worden. Neem contact op met de beheerder of probeer het opnieuw.";
    } else {
      $sql = "INSERT INTO RESERVATIONS_CAR_WASH (RESERVATION_ID, ITEM_NR, ITEM_DESC, NAME, PHONE_NR, CAR_NR) ";
      $sql .= "VALUE ";
      $sql .= "('$reservation_id','$auto_wassen_pakket','$auto_wassen_pakket_desc','$auto_wassen_name1','$auto_wassen_tel_number','$auto_wassen_kenteken_auto') ";

      $result = mysql_query($sql);
      if (!$result) {die('Could not exec query: ' . mysql_error());}
    
      $selected_date_nl = $auto_wassen_datum;
      $confirm_page = "25_confirm_reservation.php";
      $mail_template = "common/mail.message.confirm.auto.wassen.html";
      sendAskConfirmationMail();
    }
  }

  close();
  
  // redirect to the same page without the POST data
  header("Location: ".$_SERVER['PHP_SELF']."?auto_wassen_datum=$auto_wassen_datum&auto_wassen_pakket=$auto_wassen_pakket&auto_wassen_name1=$auto_wassen_name1&auto_wassen_tel_number=$auto_wassen_tel_number&auto_wassen_kenteken_auto=$auto_wassen_kenteken_auto&error=$error&location_name=$location_name");
  die;
}

?>

<?php 
include 'common/page.begin.inc.php';
?>

<body>

<div id="wrapper">

  <?php 
  $menuSelected = "diensten";
  $subMenuSelected = "auto-wassen";
  include 'common/body.header.inc.php';
  ?>

  <div id="page" class="container">
  
    <div id="content" class="main">
    
      <?php
      $contentPath = "Auto wassen &gt;&gt; Afspraak Maken";
      include 'common/body.content.header.inc.php';
      ?>

      <div class="main-top">
        <div class="first auto-wassen">
        </div>  
        <div class="second">
          <div class="content-blok">

            <?php 
            include 'common/body.error.inc.php';
            ?>

            <?php if (empty($error)) { ?>
            
              <h1>Hartelijk dank voor je reservering via RedJeZaterdag.nl!</h1>
              
              <p>  
                Je afspraak is vastgelegd op:<br />
                <?php echo $start_date->format('l d F Y');?>.
              </p>
              <p>
                Op locatie:<br />
                <?php echo $location_name;?> 
              </p>
                
              <p>  
                Om je reservering definitief te maken is er een e-mail naar je gestuurd. Klik op de link in deze e-mail om jouw reservering te bevestigen.
              </p>
              
              <p class="alert_text">
                LET OP: Om deze reservering definitief te maken moet je binnen <b>15</b> minuten op de link in deze e-mail klikken. 
                Deze werkwijze zorgt ervoor dat je <b>niet</b> met een password hoeft in te loggen en voorkomt misbruik.
              </p>
              
            <?php } ?>
      
          </div><!-- end content blok -->  
        </div><!-- end second -->  
      </div><!-- end home top -->

    </div><!-- end content -->

    <div style="clear: both;"></div>
	</div><!-- end page -->  
  
  <?php 
  include 'common/body.footer.inc.php';
  ?>
  
</div><!-- end wrapper -->

<?php 
include 'common/page.end.inc.php';
?>


